package servlet.config;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:ServletConfig学习
 * User:22954
 * Date:2024-07-14
 * Time:22:37
 */
/*
ServletConfig储存Servlet的配置信息
ServletConfig是接口,Tomcat实现的类是StandardWrapperFacade
ServletConfig是服务器的容器(Tomcat)创建的,在Servlet初始化时创建并关联
一个Servlet对应一个ServletConfig对象
 */
@WebServlet(name = "DBServlet", urlPatterns = "/dbServlet",
        initParams = {@WebInitParam(name = "username", value = "root"),
                @WebInitParam(name = "pwd", value = "mysql")})
public class DBServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = this.getServletConfig();
        System.out.println(servletConfig);
        String username = servletConfig.getInitParameter("username");
        String pwd = servletConfig.getInitParameter("pwd");
        System.out.println("username: " + username);
        System.out.println("pwd: " + pwd);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
